package cn.itcast.bos.service.system.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.dao.system.MenuRepository;
import cn.itcast.bos.domain.system.Menu;
import cn.itcast.bos.domain.system.User;
import cn.itcast.bos.service.system.MenuService;
@Service
@Transactional
public class MenuServiceImpl implements MenuService {
	@Resource
	private MenuRepository repository;
	@Override
	public List<Menu> findByParentMenuIsNull() {
		return repository.findByParentMenuIsNull();
	}
	@Override
	public void save(Menu menu) {
		if (menu.getParentMenu() !=null && menu.getParentMenu().getId() == 0) {
			menu.setParentMenu(null);
		}
		repository.save(menu);
	}
	@Override
	public List<Menu> findByUser(User user) {
		// 针对admin用户显示 所有菜单
		if (user.getUsername().equals("bao")) {
			return repository.findAll();
		} else {
			// 根据用户 角色 查询菜单
			return repository.findByUser(user.getId());
		}
	}
	
}
